package be.bonamis.umpire.ui.client.activities;

import be.bonamis.projects.umpire.business.actions.GetAllQuizA;
import be.bonamis.projects.umpire.business.actions.GetAllQuizR;
import be.bonamis.projects.umpire.business.actions.GetGamesByUmpireA;
import be.bonamis.projects.umpire.business.actions.GetGamesByUmpireR;
import be.bonamis.projects.umpire.business.actions.GetLeagueDayA;
import be.bonamis.projects.umpire.business.actions.GetLeagueDayR;
import be.bonamis.projects.umpire.business.actions.GetMonthsA;
import be.bonamis.projects.umpire.business.actions.GetMonthsR;
import be.bonamis.projects.umpire.business.actions.GetUmpiresByLDA;
import be.bonamis.projects.umpire.business.actions.GetUmpiresR;
import be.bonamis.umpire.ui.client.ClientFactory;
import be.bonamis.umpire.ui.client.mvp.DispoView;
import be.bonamis.umpire.ui.client.places.BootPlace;
import be.bonamis.umpire.ui.client.places.DispoPlace;
import be.bonamis.umpire.ui.client.places.HistoryPlace;
import be.bonamis.umpire.ui.client.places.QuizPlace;
import be.bonamis.umpire.ui.client.places.WhoPlace;
import be.credoc.common.gwt.actions.client.ActionServiceWrapper;
import be.credoc.common.gwt.actions.client.OnResponseCommand;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class DispoActivity extends BaseActivity<DispoPlace> implements DispoView.Presenter {

	private DispoView dispoView;
	private ActionServiceWrapper asw;

	public DispoActivity(DispoPlace place, ClientFactory clientFactory) {
		super(place, clientFactory);
		this.clientFactory = clientFactory;
		this.asw = clientFactory.getActionServiceWrapper();
		this.place = place;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		dispoView = clientFactory.getDispoView();
		if (place.getUmpire() != null) {
			dispoView.setValues(place.getUmpire(), place.getMonths());
		}
		dispoView.setPresenter(this);
		panel.setWidget(dispoView.asWidget());
	}

	@Override
	public void findLeagueDays(long id, int month, int year) {
		GetLeagueDayA<GetLeagueDayR> action = new GetLeagueDayA<GetLeagueDayR>();
		action.setMonth(month);
		action.setUmpireId(id);
		action.setYear(year);
		asw.execute(action, new OnResponseCommand<GetLeagueDayR>() {
			@Override
			public void execute() {
				dispoView.setLeagueDays(response.getLeagueDayDtoList());
			}
		});
	}

	@Override
	public void findUmpiresByLeagueDay(long id) {
		GetUmpiresByLDA<GetUmpiresR> action = new GetUmpiresByLDA<GetUmpiresR>();
		action.setLeagueDayId(id);
		asw.execute(action, new OnResponseCommand<GetUmpiresR>() {
			@Override
			public void execute() {
				dispoView.setUmpiresByLD(response.getUmpiresDTOList());
			}
		});

	}

	@Override
	public void exportToXls(long id) {
		Window.open(GWT.getHostPageBaseURL() + "report?leagueDayId=" + id, "_blank", "");
	}

	@Override
	public void dispoButtonClicked() {
		GetMonthsA<GetMonthsR> action = new GetMonthsA<GetMonthsR>();
		asw.execute(action, new OnResponseCommand<GetMonthsR>() {
			@Override
			public void execute() {
				clientFactory.getPlaceController().goTo(
						new DispoPlace("dispo", dispoView.getUmpire(), response.getSetDates()));
			}
		});
	}

	@Override
	public void whoButtonClicked() {
		//dispoView.setValues(dispoView.getUmpire());
		clientFactory.getPlaceController().goTo(new WhoPlace("who", dispoView.getUmpire()));
	}

	@Override
	public void quizButtonClicked() {
		GetAllQuizA<GetAllQuizR> action = new GetAllQuizA<GetAllQuizR>();
		asw.execute(action, new OnResponseCommand<GetAllQuizR>() {
			@Override
			public void execute() {
				clientFactory.getPlaceController().goTo(
						new QuizPlace("quiz", dispoView.getUmpire(), response.getQuizs()));
			}
		});
	}

	@Override
	public void historyButtonClicked() {
		GetGamesByUmpireA<GetGamesByUmpireR> action = new GetGamesByUmpireA<GetGamesByUmpireR>();
		action.setId(dispoView.getUmpire().getId());
		asw.execute(action, new OnResponseCommand<GetGamesByUmpireR>() {
			@Override
			public void execute() {
				clientFactory.getPlaceController().goTo(
						new HistoryPlace("history", dispoView.getUmpire(), response.getGames()));
			}
		});
	}

	@Override
	public void homeButtonClicked() {		
		clientFactory.getPlaceController().goTo(new BootPlace("menu", place.getUmpire()));		
	}
}
